/**
*┌──────────────────────────────────────────────────────────────┐
*│　描    述：Shop_albums接口实现                                                    
*│　作    者：NetCoreCMS                                            
*│　版    本：1.0    模板代码自动生成                                                
*│　创建时间：2019-12-25 10:52:46                             
*└──────────────────────────────────────────────────────────────┘
*┌──────────────────────────────────────────────────────────────┐
*│　命名空间： NetCoreCMS.Repository.SqlServer                                  
*│　类    名： Shop_albumsRepository                                      
*└──────────────────────────────────────────────────────────────┘
*/
using NetCoreCMS.DbHelper;
using NetCoreCMS.IRepository;
using NetCoreCMS.Models;
using Dapper;
using Microsoft.Extensions.Options;
using System;
using System.Threading.Tasks;
using NetCoreCMS.Common;
using System.Collections.Generic;
using System.Text;
using System.Linq;

namespace NetCoreCMS.Repository.SqlServer
{
    public class Shop_albumsRepository:BaseRepository<Shop_albums,Int32>, IShop_albumsRepository
    {
        public Shop_albumsRepository(IOptionsSnapshot<DbOption> options)
        {
            _dbOption =options.Get("NetCoreCMS");
            if (_dbOption == null)
            {
                throw new ArgumentNullException(nameof(DbOption));
            }
            //_dbConnection = ConnectionFactory.CreateConnection(_dbOption.DbType, _dbOption.ConnectionString);
        }
        public bool Update(List<Shop_albums> models, int shop_id, string env)
        {
            //删除已移除的图片
            DeleteList(models, shop_id, env);
            //添加/修改相册
            if (models != null)
            {
                foreach (Models.Shop_albums modelt in models)
                {
                    if (modelt.id > 0)
                    {
                        Update(modelt);
                    }
                    else
                    {
                        Insert(modelt);
                    }
                }
            }
            return true;
        }
        /// <summary>
        /// 查找不存在的图片并删除已移除的图片及数据
        /// </summary>
        public void DeleteList(List<Models.Shop_albums> models, int shop_id, string env)
        {
            StringBuilder idList = new StringBuilder();
            if (models != null)
            {
                foreach (Models.Shop_albums modelt in models)
                {
                    if (modelt.id > 0)
                    {
                        idList.Append(modelt.id + ",");
                    }
                }
            }
            string delIds = idList.ToString().TrimEnd(',');
            StringBuilder strSql = new StringBuilder();
            strSql.Append(" where id>0 and shop_id=" + shop_id);
            if (!string.IsNullOrEmpty(delIds))
            {
                strSql.Append(" and id not in(" + delIds + ")");
            }
            var ds = this.GetList(strSql.ToString()).ToList();
            foreach (Shop_albums dr in ds)
            {
                int rows = Delete(dr.id); //删除数据库
                if (rows > 0)
                {
                    FileHelper.DeleteFile(dr.thumb_path, env); //删除缩略图
                    FileHelper.DeleteFile(dr.original_path, env); //删除原图
                }
            }
        }

        /// <summary>
        /// 删除相册图片
        /// </summary>
        public void DeleteFile(List<Models.Shop_albums> models, string env)
        {
            if (models != null)
            {
                foreach (Models.Shop_albums modelt in models)
                {
                    FileHelper.DeleteFile(modelt.thumb_path, env);
                    FileHelper.DeleteFile(modelt.original_path, env);
                }
            }
        }
    }
}